import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/enter_match',
    name: 'EnterMatch',
    redirect: '/enter_match/pass',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/EnterMatch/index.vue'),
    children: [
      {
        path: 'pass',
        name: 'EnterMatchPass',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/EnterMatch/pass.vue'),
      },
      {
        path: 'add/:type',
        name: 'EnterMatchAdd',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/EnterMatch/add.vue'),
      },
    ]
  },
  {
    path: '/match_center',
    name: 'MatchEnter',
    component: () => import('../views/MatchCenter/index.vue')
  },
  {
    path: '/centre',
    name: 'Centre',
    component: () => import('../views/Centre/index.vue')
  },
  {
    path: '/forum',
    name: 'Forum',
    component: () => import('../views/Forum/index.vue')
  },
  {
    path: '/live',
    name: 'Live',
    component: () => import('../views/Live/index.vue')
  },
  {
    path: '/news/info/:id',
    name: 'NewsInfo',
    component: () => import('../views/News/info.vue'),
  },
  {
    path: '/news',
    name: 'News',
    redirect: '/news/list',
    component: () => import('../views/News/index.vue'),
    children: [
      {
        path: 'list',
        name: 'NewsList',
        component: () => import('../views/News/list.vue'),
      },
      {
        path: 'time',
        name: 'NewsTime',
        component: () => import('../views/News/time.vue'),
      }
    ]
  },
  {
    path: '/cooper',
    name: 'Cooper',
    redirect: '/cooper/support',
    component: () => import('../views/Cooper/index.vue'),
    children: [
      {
        path: 'support',
        name: 'CooperSupport',
        component: () => import('../views/Cooper/support.vue'),
      },
      {
        path: 'hold',
        name: 'CooperHold',
        component: () => import('../views/Cooper/hold.vue'),
      }
    ]
  },
  {
    path: '/exp',
    name: 'Exp',
    component: () => import('../views/Exp/index.vue')
  },
  {
    path: '/about_match',
    name: 'AboutMatch',
    redirect: '/about_match/info',
    component: () => import('../views/AboutMatch/index.vue'),
    children: [
      {
        path: 'info',
        name: 'AboutMatchInfo',
        component: () => import('../views/AboutMatch/info.vue'),
      },
      {
        path: 'question',
        name: 'AboutMatchQuestion',
        component: () => import('../views/AboutMatch/question.vue'),
      },
      {
        path: 'concat',
        name: 'AboutMatchConcat',
        component: () => import('../views/AboutMatch/concat.vue'),
      }
    ]
  },
  {
    path: '/personal',
    name: 'Personal',
    redirect: '/personal/info/own',
    component: () => import(/* webpackChunkName: "about" */ '../views/Personal/index.vue'),
    children: [
      {
        path: 'info',
        name: 'PersonalInfo',
        redirect: '/personal/info/own',
        component: () => import('../views/Personal/info.vue'),
        children: [
          {
            path: 'own',
            name: 'PersonalInfoOwn',
            meta: {
              isHome: true
            },
            component: () => import('../views/Personal/own.vue')
          },
          {
            path: 'message',
            meta: {
              isHome: true
            },
            name: 'PersonalInfoMessage',
            component: () => import('../views/Personal/message.vue')
          }
        ]
      },
      {
        path: 'motorcade',
        name: 'PersonalMotorcade',
        redirect: '/personal/motorcade/list',
        component: () => import('../views/Personal/motorcade/info.vue'),
        children: [
          {
            path: 'list',
            name: 'PersonalMotorcadeList',
            meta: {
              isHome: true
            },
            component: () => import('../views/Personal/motorcade/list.vue')
          },
          {
            path: 'create',
            meta: {
              isHome: true
            },
            name: 'PersonalCarCreate',
            component: () => import('../views/Personal/motorcade/add.vue')
          },
          {
            path: 'search',
            meta: {
              isHome: true
            },
            name: 'PersonalCarSearch',
            component: () => import('../views/Personal/motorcade/search.vue')
          }
        ]
      },
      {
        path: 'file',
        name: 'PersonalFile',
        redirect: '/personal/file/reg',
        component: () => import('../views/Personal/file/info.vue'),
        children: [
          {
            path: 'reg',
            name: 'PersonalFileReg',
            meta: {
              isHome: true,
              isBackend: true
            },
            component: () => import('../views/Personal/file/reg.vue')
          },
          {
            path: 'return',
            meta: {
              isHome: true,
              isBackend: true
            },
            name: 'PersonalFileReturn',
            component: () => import('../views/Personal/file/return.vue')
          },
          {
            path: 'drive',
            meta: {
              isHome: true,
              isBackend: true
            },
            name: 'PersonalFileDrive',
            component: () => import('../views/Personal/file/drive.vue')
          },
          {
            path: 'insurance',
            meta: {
              isHome: true,
              isBackend: true
            },
            name: 'PersonalFileInsurance',
            component: () => import('../views/Personal/file/insurance.vue')
          }
        ]
      },
      {
        path: 'reserve',
        name: 'PersonalReserve',
        redirect: '/personal/reserve/exp',
        component: () => import('../views/Personal/reserve/info.vue'),
        children: [
          {
            path: 'exp',
            name: 'PersonalReserveExp',
            meta: {
              isHome: true,
              isBackend: true
            },
            component: () => import('../views/Personal/reserve/exp.vue')
          },
          {
            path: 'forum',
            meta: {
              isHome: true,
              isBackend: true
            },
            name: 'PersonalReserveForum',
            component: () => import('../views/Personal/reserve/forum.vue')
          }
        ]
      },
    ]
  },
  {
    path: '/mc_manager/:id',
    name: 'mcManager',
    redirect: '/mc_manager/:id/info',
    component: () => import('../views/motorcadeManager/index.vue'),
    children: [
      {
        path: 'info',
        name: 'mcManagerInfo',
        meta: {
          isHome: true,
          isBackend: true
        },
        component: () => import('../views/motorcadeManager/info.vue')
      },
      {
        path: 'competition',
        name: 'mcManagerCompetition',
        meta: {
          isHome: true,
          isBackend: true
        },
        component: () => import('../views/motorcadeManager/competition.vue')
      },
      {
        path: 'user',
        name: 'mcManagerUser',
        meta: {
          isHome: true,
          isBackend: true
        },
        component: () => import('../views/motorcadeManager/user.vue')
      },
      {
        path: 'apply',
        name: 'mcManagerApply',
        meta: {
          isHome: true,
          isBackend: true
        },
        component: () => import('../views/motorcadeManager/apply.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes,
  scrollBehavior () {
    // return 期望滚动到哪个的位置
    return { x: 0, y: 0 }
  }
})

router.beforeEach((to, from, next) => {
  let token = router.app.$store.state.userinfo && router.app.$store.state.userinfo.token;
  let needAuth = to.matched.some(item => item.meta.isBackend); //返回的就是当前路由匹配到的组件类
  if(!token && needAuth) return next({path: "/"});
  next();
});

export default router
